package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.CourseMapper;
import mianyang.zhangsan.com.pojo.Course;
import mianyang.zhangsan.com.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("courseService")
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 根据id查询课程名字
     * @param courseId
     * @return
     */
    @Override
    public String queryNameById(Integer courseId) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("id", courseId);
        Course course = courseMapper.selectOne(wrapper);
        if ("".equals(course.getName()) || course.getName() == null){
            return "not found";
        }
        return course.getName();
    }

    /**
     * 根据id查询课程信息
     * @param courseId
     * @return
     */
    @Override
    public Course queryInfoById(Integer courseId) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("id", courseId);
        return courseMapper.selectOne(wrapper);
    }

    /**
     * 查询所有的课程信息
     * @return
     */
    @Override
    public List<Course> queryInfoAll() {

        return courseMapper.selectList(null);
    }

    /**
     * 分页查询所有的课程信息
     * @param page
     * @param size
     * @param teacherId
     * @return
     */
    @Override
    public Page<Course> queryByPage(int page, int size, int teacherId) {
        if(teacherId != 0){
            QueryWrapper<Course> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id", teacherId);
            return courseMapper.selectPage(new Page<>(page,size), wrapper);
        }
        return courseMapper.selectPage(new Page<>(page, size), null);
    }

    /**
     * 添加课程
     * @param course
     */
    @Override
    public void addCourse(Course course) {
        courseMapper.insert(course);
    }

    /**
     * 删除课程
     * @param id
     */
    @Override
    public void deleteCourse(int id) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        courseMapper.delete(wrapper);
    }
}
